$ontext
Main file for merit-order model

jab 01.10.2020
$offtext

*######################################################################
*@                             OPTIONS
*######################################################################
$if not set datadir $set datadir "../data/"
$if not set datafile $set datafile "merit_order_data"
$if not set outdir $set outdir "../results/"
$if not set scenario $set scenario observed
*carbon_price_constant_fuel

$set datain %datadir%%datafile%
*$set scenario carbon_price
*$set pcarb 10


*######################################################################
*@                            LOAD DATA
*######################################################################

*----------------------------------------------------------------------
*@@                       SYMBOL DECLARATIONS
*----------------------------------------------------------------------
set
    p                plants
    f                fuels and fossil technologies
    map_pf(p,f)      mapping plants to fuels
    r                renewable technologies
    t                time periods = hours
;

parameter
*   plant characteristics
    eta(p)           heat efficiency plant p [%]
    theta(p)         carbon coefficienct plant p [tCO2 per MWh]
    cap(p)           installed capacity plant p [MW]
    avail(p,t)       availability of plant p in period t [% of installed capacity]
    c(p,t)           marginal generation cost of plant p in period t
    sc(p,t)          start-up and ramping cost [Euro per MW startup]
    sc_fuel(p,t)     start-up and ramping cost in terms of fuels [MWh per MW]
    max_ramp(p,t)    maximum ramp rate

*   demand and other data
    dem(t)           demand in period t [MWh]
    pf(f,t)          fuel price in period t [Euro per MWh]
    ren(r,t)         renewable generation in period t [MWh]
    eua(t)           eua price in period t [Euro per tCO2]
    cps(t)           cps price in period t [Euro per tCO2]
    pCO2(t)          total carbon price in period t [Euro per tCO2]
    theta_carb(f)    carbon content of fuel

*   for upload
    up_ren(t,r)      upload renewables
    up_price(t,*)    upload prices
    up_gen_fos(t,f)  upload otal fossil generation
    up_gen(t,p,*)    upload plant generation
    up_units(p,f,*)  upload unit data
;
alias(t,tt);

*----------------------------------------------------------------------
*@@                       LOAD DATA
*----------------------------------------------------------------------
$onecho > temp.txt
* settings
i=%datain%.xlsx
o=%datain%.gdx

* sets
dset=p       rng=unit_characteristics!A2    rdim=1 cdim=0
dset=f       rng=unit_characteristics!B2    rdim=1 cdim=0
set=map_pf   rng=unit_characteristics!A2    rdim=2 cdim=0
set=t        rng=prices!A2                  rdim=1 cdim=0
set=r        rng=renewables!B1              rdim=0 cdim=1

* parameters
par=dem      rng=demand!A2                  rdim=1 cdim=0
par=up_ren   rng=renewables!A1              rdim=1 cdim=1
par=up_price     rng=prices!A1                  rdim=1 cdim=1
par=up_gen_fos   rng=generation_total_fossil!A1 rdim=1 cdim=1
par=up_gen       rng=generation!A1              rdim=2 cdim=1
par=up_units     rng=unit_characteristics!A1    rdim=2 cdim=1
$offecho

* if input gdx does not exists create it
$if not exist %datain%.gdx $call gdxxrw @temp.txt
execute "rm temp.txt";
$gdxin %datain%.gdx
$loaddc p f map_pf t r
$loaddc up_ren up_price dem up_gen_fos up_gen up_units
$gdxin

*----------------------------------------------------------------------
*@@                       ASSIGNMENTS
*----------------------------------------------------------------------
avail(p,t) = up_gen(t,p,"availability");
cap(p) = sum(map_pf(p,f), up_units(p,f, "Capacity"));
theta(p) = sum(map_pf(p,f), up_units(p,f, "carbon_coef"));
eta(p) = sum(map_pf(p,f), up_units(p,f, "eta"));

ren(r,t) = up_ren(t,r);
pf(f,t) = up_price(t,f);
eua(t) = up_price(t,"eua");
cps(t) = up_price(t,"cps");
pCO2(t) = eua(t) + cps(t);

* calculate marginal generation cost
c(p,t) =  sum(map_pf(p,f), pf(f,t))/eta(p)
          + pCO2(t)*theta(p);

* test availability to ensure feasibility
parameter
    test_avail(p,t) test that availbility allows for observed generation
;
test_avail(p,t)$(avail(p,t)*cap(p)<up_gen(t,p,"fpn") ) =
    round(avail(p,t)*cap(p) - up_gen(t,p,"fpn"), 1e-6);
abort$(sum((p,t), test_avail(p,t))) "##### AVAILBILITY TEST FAILED PLEASE CHECK",
     test_avail;

* make up some ramping cost
* see Schröder et al. 2013 p. 63, 
sc(p,t) = 0;
sc_fuel(p,t) = 0;
sc(p,t)$map_pf(p,"coal") = 10;
sc(p,t)$map_pf(p,"gas") = 1.5;
sc_fuel(p,t)$map_pf(p,"coal") = 6.2;
sc_fuel(p,t)$map_pf(p,"gas") = 3.5;
theta_carb("coal") = 0.34;
theta_carb("gas") = 0.2;

* load gradient
max_ramp(p,t) = 0;

*######################################################################
*@                              MODEL
*######################################################################

*----------------------------------------------------------------------
*@@                     VARIABLE AND EQUATION DECLARATIONS
*----------------------------------------------------------------------
Variable
    COST        total system cost [Euro]
;

Positive Variable
    GEN(p,t)            generation of plant p in period t [MWh]
    SLACK(t)
    GEN_UP(p,t)         generation change upward
    GEN_DOWN(p,t)       generation change downward
    GEN_MIN(p,t)        generation below minimum generation
;

Equation
    obj                 objective definition: total system cost
    mkt_ele(t)          market clearing electricity in period t
    mkt_cap(p,t)        market clearing available capacity in period t plant p
    def_gen_up(p,t)     defínition generation started
    def_gen_down(p,t)   definition generation shut-down
    lom_gen_online(p,t) law of motion generation
    def_gen_min(p,t)    definition generation below minimum generation
    res_ramping(p,t)    ramp rate restriction
;

*----------------------------------------------------------------------
*@@                        EQUATION ASSIGNMENT
*----------------------------------------------------------------------
* objective: total system cost
obj..
    COST               =E= sum((p,t),
*                             Fuel and carbon cost 	
                              ( sum(map_pf(p,f), pf(f,t))/eta(p)
                                + pCO2(t)*theta(p)
                              )*GEN(p,t)
*                             Ramping cost (but not for first period)
	                      + ( sc(p,t)
				  + sc_fuel(p,t)*sum(map_pf(p,f),
				    pf(f,t) + theta_carb(f)*pCO2(t))
			         )*GEN_UP(p,t)$(ord(t) gt 1)
                            )
*                           + sum(t, SLACK(t))*10000
;

* market clearing electricity
mkt_ele(t)..
    sum(p, GEN(p,t))
    + sum(r, ren(r,t)) =G= dem(t)
*- SLACK(t)
;

* market clearing capacity
mkt_cap(p,t)..
    cap(p)*avail(p,t)  =G= GEN(p,t)
;

*@@@ --------------------- RAMPING CONSTRAINT -------------------------
* definition generation started
def_gen_up(p,t)$(sc(p,t) or max_ramp(p,t))..
    GEN_UP(p,t)        =G= GEN(p,t) - GEN(p,t-1)
;

* definition generation shut-down
def_gen_down(p,t)$(sc(p,t) or max_ramp(p,t))..
    GEN_DOWN(p,t)      =G= GEN(p,t-1) - GEN(p,t)
;

* law of motion generation
lom_gen_online(p,t)$(sc(p,t) or max_ramp(p,t))..
    GEN(p,t)           =E= GEN(p,t-1) + GEN_UP(p,t)
                           - GEN_DOWN(p,t)
;

* ramp rate restriction
res_ramping(p,t)$max_ramp(p,t)..
    max_ramp(p,t)*cap(p)
                       =G= GEN_UP(p,t) - GEN_DOWN(p,t)
;

*----------------------------------------------------------------------
*@@                          MODEL ASSIGNMENT
*----------------------------------------------------------------------
model merit_order merit order model of UK market
      /obj, mkt_ele, mkt_cap, 
       def_gen_up, def_gen_down, lom_gen_online, res_ramping/
;

*######################################################################
*@                        SIMULATIONS
*######################################################################
$include %scenario%




